package com.nbcio.pm.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.nbcio.common.core.domain.R;
import com.nbcio.common.core.validate.AddGroup;
import com.nbcio.common.core.validate.EditGroup;
import com.nbcio.common.core.validate.QueryGroup;
import com.nbcio.common.idempotent.annotation.RepeatSubmit;
import com.nbcio.common.log.annotation.Log;
import com.nbcio.common.log.enums.BusinessType;
import com.nbcio.common.mybatis.core.page.PageQuery;
import com.nbcio.common.mybatis.core.page.TableDataInfo;
import com.nbcio.common.web.core.BaseController;
import com.nbcio.pm.domain.PmTeam;
import com.nbcio.pm.domain.bo.PmTeamBo;
import com.nbcio.pm.domain.vo.PmTeamVo;
import com.nbcio.pm.service.IPmTeamService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import java.util.List;

/**
 * 团队管理
 *
 * @author nbacheng
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/pm/team")
public class PmTeamController extends BaseController {

    private final IPmTeamService pmTeamService;

    /**
     * 查询团队管理列表
     */
    @SaCheckPermission("pm:team:query")
    @GetMapping("/list")
    public TableDataInfo<PmTeamVo> list(PmTeamBo bo, PageQuery pageQuery) {
        return pmTeamService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出团队管理列表
     */
    @SaCheckPermission("pm:team:export")
    @Log(title = "团队管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(PmTeamBo bo, HttpServletResponse response) {
        List<PmTeamVo> list = pmTeamService.queryList(bo);
        // TODO: 实现导出功能
    }

    /**
     * 获取团队管理详细信息
     */
    @SaCheckPermission("pm:team:query")
    @GetMapping("/{teamId}")
    public R<PmTeamVo> getInfo(@NotNull(message = "主键不能为空")
                               @PathVariable Long teamId) {
        return R.ok(pmTeamService.queryById(teamId));
    }

    /**
     * 新增团队管理
     */
    @SaCheckPermission("pm:team:add")
    @Log(title = "团队管理", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody PmTeamBo bo) {
        return toAjax(pmTeamService.insertByBo(bo));
    }

    /**
     * 修改团队管理
     */
    @SaCheckPermission("pm:team:edit")
    @Log(title = "团队管理", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody PmTeamBo bo) {
        return toAjax(pmTeamService.updateByBo(bo));
    }

    /**
     * 删除团队管理
     */
    @SaCheckPermission("pm:team:remove")
    @Log(title = "团队管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{teamIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] teamIds) {
        return toAjax(pmTeamService.deleteWithValidByIds(List.of(teamIds), true));
    }

    /**
     * 根据团队编码查询团队
     */
    @SaCheckPermission("pm:team:query")
    @GetMapping("/code/{teamCode}")
    public R<PmTeamVo> getByTeamCode(@PathVariable String teamCode) {
        return R.ok(pmTeamService.queryByTeamCode(teamCode));
    }

    /**
     * 根据团队负责人查询团队列表
     */
    @SaCheckPermission("pm:team:query")
    @GetMapping("/leader/{teamLeaderId}")
    public R<List<PmTeamVo>> getByTeamLeader(@PathVariable Long teamLeaderId) {
        return R.ok(pmTeamService.queryByTeamLeader(teamLeaderId));
    }

    /**
     * 根据团队类型查询团队列表
     */
    @SaCheckPermission("pm:team:query")
    @GetMapping("/type/{teamType}")
    public R<List<PmTeamVo>> getByTeamType(@PathVariable String teamType) {
        return R.ok(pmTeamService.queryByTeamType(teamType));
    }

    /**
     * 检查团队编码是否唯一
     */
    @SaCheckPermission("pm:team:query")
    @GetMapping("/checkTeamCodeUnique")
    public R<Boolean> checkTeamCodeUnique(String teamCode, Long teamId) {
        return R.ok(pmTeamService.checkTeamCodeUnique(teamCode, teamId));
    }

    /**
     * 检查团队名称是否唯一
     */
    @SaCheckPermission("pm:team:query")
    @GetMapping("/checkTeamNameUnique")
    public R<Boolean> checkTeamNameUnique(String teamName, Long teamId) {
        return R.ok(pmTeamService.checkTeamNameUnique(teamName, teamId));
    }
}
